import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PordComsumerdemo {
    public static void main(String[] args) {
        MyPrint myPrint = new MyPrint();
        new Thread(() -> {
            for (int i = 0; i < 26; i++) myPrint.printNumbers();
        }, "t1: ").start();
        new Thread(() -> {
            for (int i = 0; i < 26; i++) myPrint.printLetters();
        }, "t2: ").start();
    }
}

class MyPrint {

    private int number = 0;
    private int numbers = 0;
    private int letter = 0;

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void printNumbers() {
        lock.lock();
        try {

            //判断 防止虚假唤醒用while
            while (numbers != letter) {
                condition.await();
            }
            //干活
            numbers++;
            System.out.println(Thread.currentThread().getName() + (++number));
            System.out.println(Thread.currentThread().getName() + (++number));
            //通知
            condition.signalAll();

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void printLetters() {
        lock.lock();
        try {
            while (numbers == letter) {
                condition.await();
            }
            System.out.println(Thread.currentThread().getName() + (char) (++letter + 96));
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

}